/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fri.daolayer;

import fri.businessLayer.HistoryBeanRemote;
import fri.entiry.FrHistory;
import fri.entiry.FrUser;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.naming.Context;
import javax.naming.InitialContext;

/**
 *
 * @author NguyenDongUIT
 */
public class HistoryDAO {
    
    public static List<FrHistory> getHistory(String userId) {
        try {

            Context ic = new InitialContext();
            HistoryBeanRemote rm = (HistoryBeanRemote) ic.lookup("fri.businessLayer.HistoryBeanRemote");
            return rm.getHistory(userId);

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
        }
        return null;
    }
    
    public static List<FrHistory> getHistoryByUserIdAndState(String userId, int state) {
        try {

            Context ic = new InitialContext();
            HistoryBeanRemote rm = (HistoryBeanRemote) ic.lookup("fri.businessLayer.HistoryBeanRemote");
            return rm.getHistoryByUserIdAndState(userId, state);

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
        }
        return null;
    }
    
    public static int addHistory(String userId, String content, int status) {
        try {

            Context ic = new InitialContext();
            HistoryBeanRemote rm = (HistoryBeanRemote) ic.lookup("fri.businessLayer.HistoryBeanRemote");
            FrHistory history = new FrHistory();
            UUID u=UUID.randomUUID();
            history.setHistoryId(u.toString());
            FrUser user=new FrUser();
            user.setUserId(userId);
            history.setUserId(user);
            history.setContent(content);
            history.setStatus(status);
            history.setDateCreated(new Date());
            
            
            return rm.addHistory(history);

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
        }
        return 0;
    }
    
    public static int deleteHistory(String historyId) {
        try {

            Context ic = new InitialContext();
            HistoryBeanRemote rm = (HistoryBeanRemote) ic.lookup("fri.businessLayer.HistoryBeanRemote");

            return rm.deleteHistory(historyId);

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
        }
        return 0;
    }
    
    public static int deleteAllHistory(String historyId) {
        try {
            int state = 1;
            Context ic = new InitialContext();
            HistoryBeanRemote rm = (HistoryBeanRemote) ic.lookup("fri.businessLayer.HistoryBeanRemote");

            return rm.deleteAllHistory(historyId, state);

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
        }
        return 0;
    }
    
    public static int isHistoryFinished(String historyId) {
        try {

            Context ic = new InitialContext();
            HistoryBeanRemote rm = (HistoryBeanRemote) ic.lookup("fri.businessLayer.HistoryBeanRemote");

            return rm.isHistoryFinished(historyId);

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
        }
        return 0;
    }
    
    public static int changeStateHistory(String historyId, int state) {
        try {

            Context ic = new InitialContext();
            HistoryBeanRemote rm = (HistoryBeanRemote) ic.lookup("fri.businessLayer.HistoryBeanRemote");

            return rm.changeStateHistory(historyId, state);

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
        }
        return 0;
    }
    
    public static FrHistory getHistoryById(String historyId) {
         try {

            Context ic = new InitialContext();
            HistoryBeanRemote rm = (HistoryBeanRemote) ic.lookup("fri.businessLayer.HistoryBeanRemote");

            return rm.getHistoryById(historyId);

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
        }
        return null;
    }
}
